package vashion.azeroth.api.controller.member;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import vashion.azeroth.api.controller.base.BaseController;
import vashion.azeroth.contant.AzerothConstants;
import vashion.azeroth.contant.GenericErrorEnum;
import vashion.azeroth.contant.GroupErrorEnum;
import vashion.azeroth.core.manager.personal.PersonalGroupManager;
import vashion.azeroth.core.manager.personal.PersonalGroupMemberManager;
import vashion.azeroth.core.manager.personal.PersonalMemberManager;
import vashion.azeroth.core.personal.pojo.PersonalGroup;
import vashion.azeroth.core.personal.pojo.PersonalGroupMember;
import vashion.azeroth.core.personal.pojo.PersonalMember;
import vashion.azeroth.core.result.ApiResult;
import vashion.azeroth.core.service.PersonalMemberService;

@Controller
@RequestMapping("/member")
public class GroupController extends BaseController {

	@Autowired
	private PersonalMemberService<PersonalMember> memberService;
	@Autowired
	private PersonalMemberManager memberManager;
	@Autowired
	private PersonalGroupManager groupManager;
	@Autowired
	private PersonalGroupMemberManager groupMemberManager;
	
	@RequestMapping("createGroup.htm")
	@ResponseBody
	public ApiResult<String> createGroup(@ModelAttribute("group") PersonalGroup group) {
		
		if(null == group.getSellerMemberId()) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(), "用户ID" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
		}
		
		if(StringUtils.isBlank(group.getGroupName())) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(), "组名" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
		}
		
		PersonalMember member = memberService.get(group.getSellerMemberId());
		if(null == member) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EXIST.getErrorCode(), "用户" + GenericErrorEnum.NOT_EXIST.getErrorMessage());
		}
		if(!member.getType().equals(AzerothConstants.PersonalMember.MemberType.supplier)) {
			return ApiResult.newErrorResult(GroupErrorEnum.NOT_CREATE.getErrorCode(), GroupErrorEnum.NOT_CREATE.getErrorMessage());
		}
		
		group.setSellerMemberId(member.getMemberId());
		group.setSellerMemberName(member.getMemberName());
		group.setState(AzerothConstants.PersonalGroup.PersonalGroupState.enable);
		group.setTimeAdd(new Date());
		group.setTimeEdit(new Date());
		
		groupManager.save(group);
		
		return ApiResult.newSuccessResult();
	}
	
	@RequestMapping("modifyGroup.htm")
	@ResponseBody
	public ApiResult<String> modifyGroup(@ModelAttribute("group") PersonalGroup group) {
		
		if(null == group.getGroupId()) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(), "组ID" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
		}
		
//		if(null == group.getSellerMemberId()) {
//			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(), "用户ID" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
//		}
//		
//		PersonalMember member = memberService.get(group.getSellerMemberId());
//		if(null == member) {
//			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EXIST.getErrorCode(), "用户" + GenericErrorEnum.NOT_EXIST.getErrorMessage());
//		}
//		
//		group.setSellerMemberId(member.getMemberId());
//		group.setSellerMemberName(member.getMemberName());
		group.setTimeEdit(new Date());
		
		groupManager.update(group);
		
		return ApiResult.newSuccessResult();
	}
	
	@RequestMapping("queryGroup.htm")
	@ResponseBody
	public ApiResult<List<PersonalGroup>> queryGroup(@ModelAttribute("group") PersonalGroup group) {
		
		if(null == group.getSellerMemberId()) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(), "用户ID" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
		}
		
		PersonalMember member = memberService.get(group.getSellerMemberId());
		if(null == member) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EXIST.getErrorCode(), "用户" + GenericErrorEnum.NOT_EXIST.getErrorMessage());
		}
		
		group.setSellerMemberId(member.getMemberId());
		group.setState(AzerothConstants.PersonalGroup.PersonalGroupState.enable);
		
		List<PersonalGroup> groups = groupManager.findAll(group);
		
		return ApiResult.newSuccessResult(groups);
	}
	
	@RequestMapping("queryGroupMember.htm")
	@ResponseBody
	public ApiResult<List<PersonalMember>> queryGroupMember(@ModelAttribute("group") PersonalGroupMember groupMember) {
		
		if(null == groupMember.getGroupId()) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(), "组ID" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
		}
		
		List<PersonalMember> agents = memberManager.queryGroupMember(groupMember.getGroupId());
		
		return ApiResult.newSuccessResult(agents);
	}
	
	@RequestMapping("addGroupMember.htm")
	@ResponseBody
	public ApiResult<String> addGroupMember(@ModelAttribute("groupMember") PersonalGroupMember groupMember) {
		
		if(null == groupMember.getGroupId()) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(), "组ID" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
		}
		
		if(null == groupMember.getBuyerMemberId()) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(), "用户ID" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
		}
		PersonalMember buyerMember = memberService.get(groupMember.getBuyerMemberId());
		if(null == buyerMember) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EXIST.getErrorCode(), "用户" + GenericErrorEnum.NOT_EXIST.getErrorMessage());
		}
		
		groupMember.setBuyerMemberId(buyerMember.getMemberId());
		groupMember.setBuyerMemberName(buyerMember.getMemberName());
		groupMember.setGroupId(groupMember.getGroupId());
		groupMember.setState(AzerothConstants.PersonalGroup.PersonalGroupState.enable);
		groupMember.setTimeAdd(new Date());
		groupMember.setTimeEdit(new Date());
		
		groupMemberManager.save(groupMember);
		
		return ApiResult.newSuccessResult();
	}
	
	@RequestMapping("removeGroupMember.htm")
	@ResponseBody
	public ApiResult<String> removeGroupMember(@ModelAttribute("groupMember") PersonalGroupMember groupMember) {
		
		if(null == groupMember.getGroupId()) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(), "组ID" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
		}
		
		if(null == groupMember.getBuyerMemberId()) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(), "用户ID" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
		}
		PersonalMember buyerMember = memberService.get(groupMember.getBuyerMemberId());
		if(null == buyerMember) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EXIST.getErrorCode(), "用户" + GenericErrorEnum.NOT_EXIST.getErrorMessage());
		}
		
		groupMember.setBuyerMemberId(buyerMember.getMemberId());
		groupMember.setGroupId(groupMember.getGroupId());
		groupMember.setState(AzerothConstants.PersonalGroup.PersonalGroupState.disable);
		groupMember.setTimeEdit(new Date());
		
		groupMemberManager.update(groupMember);
		
		return ApiResult.newSuccessResult();
	}
	
	//删除组及组内成员
	@RequestMapping("removeGroup.htm")
	@ResponseBody
	public ApiResult<String> removeGroupMember(@ModelAttribute("group") PersonalGroup group) {
		
		if(null == group.getGroupId()) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(), "组ID" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
		}
		
		if(null == group.getSellerMemberId()) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(), "用户ID" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
		}
		PersonalMember sellerMember = memberService.get(group.getSellerMemberId());
		if(null == sellerMember) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EXIST.getErrorCode(), "用户" + GenericErrorEnum.NOT_EXIST.getErrorMessage());
		}
		
		PersonalGroup grouphis = groupManager.get(group.getGroupId());
		if(null == grouphis) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EXIST.getErrorCode(), "组" + GenericErrorEnum.NOT_EXIST.getErrorMessage());
		}
		
		if(!grouphis.getSellerMemberId().equals(group.getSellerMemberId())) {
			return ApiResult.newErrorResult(GroupErrorEnum.NOT_PERMISSION.getErrorCode(), GroupErrorEnum.NOT_PERMISSION.getErrorMessage());
		}
		
		PersonalGroupMember groupMember = new PersonalGroupMember();
		groupMember.setGroupId(group.getGroupId());
		groupMember.setState(AzerothConstants.PersonalGroupMember.PersonalGroupMemberState.disable);
		groupMember.setTimeEdit(new Date());
		groupMemberManager.update(groupMember);
		
		
		group.setState(AzerothConstants.PersonalGroup.PersonalGroupState.disable);
		group.setTimeEdit(new Date());
		groupManager.update(group);
		
		return ApiResult.newSuccessResult();
	}
	
	@RequestMapping("queryNoGroupAgent.htm")
	@ResponseBody
	public ApiResult<List<PersonalMember>> queryNoGroupAgent(Long memberId) {
		
		if(null == memberId) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EMPTY.getErrorCode(), "用户ID" + GenericErrorEnum.NOT_EMPTY.getErrorMessage());
		}
		
		PersonalMember sellerMember = memberService.get(memberId);
		if(null == sellerMember) {
			return ApiResult.newErrorResult(GenericErrorEnum.NOT_EXIST.getErrorCode(), "用户" + GenericErrorEnum.NOT_EXIST.getErrorMessage());
		}
		
		List<PersonalMember> memberAgents = memberManager.findNoGroupAgent(memberId);
		
		return ApiResult.newSuccessResult(memberAgents);
	}
	
	
	
}
